package com.pdfbox.sample.quickstart;

import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;

import org.apache.pdfbox.Loader;
import org.apache.pdfbox.multipdf.Splitter;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;

public class _13SplitPdf {

  public static void main (String args[]) throws IOException {
    String existFileName = "E:/PdfBox_Examples/周报_add_multiple_text.pdf";
    String exportFileName = "E:/PdfBox_Examples/sample_";

    // 第1步:加载现有PDF文档
    File file = new File(existFileName);
//    PDDocument document = PDDocument.load(file);
    PDDocument document = Loader.loadPDF(file);

    // 第2步:实例化Splitter类
    Splitter splitter = new Splitter();

    // 第3步:拆分PDF文档
    List<PDDocument> Pages = splitter.split(document);

    // 第4步:创建迭代器对象
    Iterator<PDDocument> iterator = Pages.listIterator();

    // Saving each page as an individual document
    int i = 1;
    while(iterator.hasNext()) {
      PDDocument pd = iterator.next();
      pd.save(exportFileName + i++ +".pdf");
    }
    System.out.println("Multiple PDF’s created");

    // 第5步:关闭文档
    document.close();

    System.out.println("export " + exportFileName + " success");
  }

}
